package cn.thr.pethome.systemdictionarytype.controller;

import cn.thr.pethome.systemdictionarytype.domain.SystemDictionaryType;
import cn.thr.pethome.systemdictionarytype.query.SystemDictionaryTypeQuery;
import cn.thr.pethome.systemdictionarytype.service.ISystemDictionaryTypeService;
import cn.thr.pethome.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "字典系统",description="字典相关的CRUD功能")
@RequestMapping("/system")
public class SystemDictionaryTypeController {

    @Autowired
    private ISystemDictionaryTypeService iSystemDictionaryTypeService;

    @GetMapping("/list")
    @ApiOperation(value = "获取所有的字典信息" )
    public List<SystemDictionaryType> findAll(){
        return iSystemDictionaryTypeService.findAll();
    }

    //@GetMapping 查询一个发送get请求  "/{id}" 这个id只是一个占位符 等会这个id会传给Long id
    @GetMapping("/{id}")
    @ResponseBody
    @ApiOperation(value = "这个是用id来查询字典信息")
    //@PathVariable("id") 从path 路径中获取到的id 返回给Long id
    public AjaxResult findOne(@PathVariable("id") Long id){
        try {
            SystemDictionaryType one = iSystemDictionaryTypeService.findOne(id);
            return AjaxResult.success().setData(one).setMsg("查找到一坨狗屎");
            //通过工具类返回信息是否成功或者失败  setData(one)是用来返回数据 setMsg修改要显示的文字
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("来嘛，哦豁了嘛");
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    @ApiOperation(value = "这是根据id删除数据")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            iSystemDictionaryTypeService.delete(id);
            return AjaxResult.success().setMsg("你猜下删除成功没有");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }

    /**
     *
     * @RequestBody 是前端传进来
     * @ResponseBody 是后端返回给前端的
     *
     */
    @PutMapping
    @ResponseBody //前端传后端
    @ApiOperation(value = "如果有id的话就是修改，没有id的情况下就是添加")
    public AjaxResult save(@RequestBody SystemDictionaryType systemDictionaryType){
        try {
            if (systemDictionaryType.getId()!=null) {
                iSystemDictionaryTypeService.update(systemDictionaryType);
            }else {
                iSystemDictionaryTypeService.add(systemDictionaryType);
            }
            return AjaxResult.success().setMsg("你猜成功没得");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }


    /**
     *
     * @RequestBody 是前端传进来
     * @ResponseBody 是后端返回给前端的
     *
     */
    @PostMapping("/pageList")
    @ResponseBody
    @ApiOperation(value = "用来做分页查询的")
    public AjaxResult findQuery(@RequestBody SystemDictionaryTypeQuery query){
        try {
            //获取到当前页
            List<SystemDictionaryType> byQuery = iSystemDictionaryTypeService.findByQuery(query);
            //这是获取到总条数
            Long count = iSystemDictionaryTypeService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
            //setData 用来返回数据的   setTotal 用来返回总条数
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PatchMapping("/del")
    @ResponseBody
    @ApiOperation(value = "这是用来批量删除的")
    //@RequestBody 把后端都数据返回给前端
    public  AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            iSystemDictionaryTypeService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }
}
